import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import java.util.Calendar;
import java.util.TimeZone;
import java.util.concurrent.*;

public class SimulaNotificacionesEagleEye {
	public static void main(String[] args) {
		if (args.length < 1) {
			System.out
					.println("debe pasar el parametro:  puerto");
			System.exit(-1);
		}

		int puerto = Integer.parseInt(args[0]);
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(puerto);

		} catch (IOException e) {
			System.err.println("No se puede escuchar en puerto: " + puerto
					+ ". " + e.getMessage());
			System.exit(-1);
		}		

		while (true) {
			try {
				new HiloNotificacion(serverSocket.accept()).start();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}

class HiloNotificacion extends Thread {
	private Socket socketCliente;

	public HiloNotificacion(Socket cliente) {
		this.socketCliente = cliente;
	}


	public void run() {
		try {
			//System.out.println(Thread.currentThread().getName()+"-"+Calendar.getInstance(TimeZone.getTimeZone("America/Lima")).getTime());
			
			BufferedReader in = new BufferedReader(new InputStreamReader(
					socketCliente.getInputStream()));

			String mensaje = in.readLine();
			Thread.sleep(200);
			
			long timeActual = System.currentTimeMillis();
			long timestamp = Long.parseLong(mensaje);
			
			
			System.out.println(timeActual-timestamp );
			//System.out.println("FECHA"+fecha);
			in.close();
			this.socketCliente.close();
		} catch (IOException e2) {
			e2.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
